package top.csmcool.musicapi.client;

/*
 * @description
 * @date 2025/1/31
 * @author shijin
 * @version 1.0
 */

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import top.csmcool.musicapi.enums.PlatformEnum;
import top.csmcool.musicapi.model.api.qq.*;
import top.csmcool.musicapi.util.MusicRequestUtil;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

@Component
@RequiredArgsConstructor
public class QQClient extends CommonClient{

    private final MusicRequestUtil musicRequestUtil;

    public List<Song> songlist(JSONObject parameter) {
        parameter.set(getMETHOD_NAME(),"songlist");
        JSONObject jsonObject = musicRequestUtil.sendRequestAutowiredJson(parameter, getUrl(parameter));
        if(jsonObject == null ||
                jsonObject.isEmpty() ||
                jsonObject.getJSONObject("data") == null ||
                jsonObject.getJSONObject("data")
                        .getJSONArray("songlist") == null) {
            return null;
        }
        JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("songlist");
        return JSONUtil.toList(jsonArray, Song.class);
    }

    public UserSongList userSonglist(JSONObject parameter) {
        UserSongList userSongList = new UserSongList();
        parameter.set(getMETHOD_NAME(),"userSonglist");
        JSONObject jsonObject = musicRequestUtil.sendRequestAutowiredJson(parameter, getUrl(parameter));
        if(jsonObject == null
                || jsonObject.getJSONObject("data") == null
                || jsonObject.getJSONObject("data").getJSONArray("list") == null) {
            return userSongList;
        }
        JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("list");
        HostInfo creator = jsonObject.getJSONObject("data").getBean("creator",HostInfo.class);
        List<MusicDirectory> list = JSONUtil.toList(jsonArray, MusicDirectory.class);
        userSongList.setCreator(creator);
        userSongList.setList(list);
        return userSongList;
    }

    public List<MusicPlayList> recommendPlaylistU(JSONObject parameter) {
        parameter.set(getMETHOD_NAME(),"recommendPlaylistU");
        JSONObject jsonObject = musicRequestUtil.sendRequestAutowiredJson(parameter, getUrl(parameter));
        if(jsonObject == null
                || jsonObject.getJSONObject("data") == null
                || jsonObject.getJSONObject("data").getJSONArray("list") == null) {
            return new LinkedList<>();
        }
        JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("list");
        return JSONUtil.toList(jsonArray, MusicPlayList.class);
    }



    public String lyric(JSONObject parameter) {
        parameter.set(getMETHOD_NAME(),"lyric");
        JSONObject lyric = musicRequestUtil.sendRequestAutowiredJson(parameter, getUrl(parameter));
        if(lyric != null && lyric.get("data") != null && lyric.getJSONObject("data").get("lyric") != null) {
            return lyric.getJSONObject("data").getStr("lyric");
        }
        return null;
    }

    public List<Song> search(JSONObject parameter) {
        parameter.set(getMETHOD_NAME(),"search");
        JSONObject jsonObject = musicRequestUtil.sendRequestAutowiredJson(parameter, getUrl(parameter));
        if(jsonObject == null
                || jsonObject.getJSONObject("data") == null
                || jsonObject.getJSONObject("data").getJSONArray("list") == null) {
            return new LinkedList<>();
        }
        JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("list");
        return JSONUtil.toList(jsonArray, Song.class);
    }

    public JSONObject songUrls(JSONObject parameter) {
        parameter.set(getMETHOD_NAME(),"songUrls");
        JSONObject entries = musicRequestUtil.sendRequestAutowiredJson(parameter, getUrl(parameter));
        if(entries == null
                || entries.isEmpty()
                || entries.get("data") == null) {
            return null;
        }
        return entries.getJSONObject("data");
    }

    public String songUrl(JSONObject parameter) {
        parameter.set(getMETHOD_NAME(),"songUrl");
        JSONObject entries = musicRequestUtil.sendRequestAutowiredJson(parameter, getUrl(parameter));
        if(entries == null
                || entries.isEmpty()
                || entries.get("data") == null) {
            return null;
        }
        return entries.getStr("data");
    }

    @Override
    public PlatformEnum getPlatform() {
        return PlatformEnum.QQ;
    }
}
